<?php
namespace app\home\model;
use think\Model;

class CourseModel extends Model
{
	protected $table  = 'qm_course';


	/**
	*
	*首页默认5个课程
	**/
	public function getIndexCourseList($field = '*', $page = 1)
	{
		$keys = (($page - 1) * 5);
		$limit = 5;

		$date = date('Y-m-d H:i:s');
		$where = [
			['status', '=', 1],
			['start_time', '<=', $date],
			['end_time', '>=', $date],
		];

		$list = $this->where($where)->field($field)->order('sort DESC, add_time DESC')->limit($keys, $limit)->select();
		if(!empty($list))
		{
			$list = $list->toArray();
		}
		return $list;
	}



	/**
	*
	*标题搜索-课程列表
	**/
	public function getSeekCourseList($title, $field = '*', $page = 1, $limit = 10)
	{
		$keys = (($page - 1) * $limit);

		$date = date('Y-m-d H:i:s');
		$where = [
			['title', 'like', '%'.$title.'%'],
			['status', '=', 1],
			['start_time', '<=', $date],
			['end_time', '>=', $date],
		];

		$list = $this->where($where)->field($field)->order('sort DESC, add_time DESC')->limit($keys, $limit)->select();
		if(!empty($list))
		{
			$list = $list->toArray();
		}
		return $list;
	}



	/**
	*
	*根据课程ID查找课程信息
	**/
	public function getCourseInfoDetails($course_id, $field = '*')
	{

		$where = [
			['id', '=', $course_id],
		];

		$list = $this->where($where)->field($field)->find();
		return $list;
	}



	/**
	*
	*更新课程信息
	**/
	public function updateCourse($where, $update)
	{
		$res = $this->where($where)->update($update);
		return $res;
	}



	/**
	*
	*0元学-课程 or 体验课
	*
	**/
	public function getzeroMoneyStudyList($page = 1, $field = '*', $type = 1)
	{
		$keys = (($page - 1) * 10);
		$limit = 10;
		$date = date('Y-m-d H:i:s');
		$where = [
			['c.status', '=', 1],
			['c.start_time', '<=', $date],
			['c.end_time', '>=', $date],
		];

		//等于2，就是体验课
		if($type == 1){
			$where[] = ['cl.label', '=', '0元学'];
		}else{
			$where[] = ['cl.label', '=', '体验课'];
		}

		//开始查找
		$list = $this->alias('c')->join('qm_course_label cl', 'c.id = cl.course_id')->where($where)->field($field)->order('c.sort DESC, c.add_time DESC')->limit($keys, $limit)->select();
		if(!empty($list))
		{
			$list = $list->toArray();
		}

		return $list;
	}



	//0.01元，购买课程升级活动
	public function zeroMoneyStudyUpdateGrade($page = 1, $field = '*')
	{
		
		$limit = 4;
		$date = date('Y-m-d H:i:s');
		$where = [
			['sell_money', '=', 0.01],
			['status', '=', 1],
			['start_time', '<=', $date],
			['end_time', '>=', $date],
		];

		//开始查找
		$list = $this->where($where)->field($field)->order('sort DESC, add_time DESC')->limit(0, $limit)->select();
		if(!empty($list))
		{
			$list = $list->toArray();
		}
		return $list;
	}	
}
